package com.ufaud.ais.sys.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import com.ufaud.ais.pub.entity.BaseEntity;

/**
 * 功能：自定义项
 * 
 * @author 林可
 * @version 1.0 2010-7-21
 */
@Entity
@Table(name = "custom_item")
@NamedQueries ({
    @NamedQuery(
    		name = "getItemListByTypeId",
    		query = "select ci from CustomItem ci" +
    				" where ci.itemType.itemTypeId = :typeId"
	),
    @NamedQuery(
    		name = "getItemListByTypeName",
            query = "select ci from CustomItem ci" +
            		" where ci.itemType.itemTypeName = :typeName"
	)
})
public class CustomItem extends BaseEntity<Long>{

	private Long itemId;
	private String itemName;
	private String itemDesc;
	private CustomItemType itemType;
	private CustomItem parentItem;
	private Set<CustomItem> childrenSet = new HashSet<CustomItem>();

	@Id
	@GeneratedValue
	@Column(name = "item_id")
	public Long getItemId() {
		return itemId;
	}

	@Column(name = "item_name")
	public String getItemName() {
		return itemName;
	}

	@Column(name = "item_desc")
	public String getItemDesc() {
		return itemDesc;
	}

	@ManyToOne(fetch = FetchType.LAZY, targetEntity = CustomItemType.class)
	@JoinColumn(name = "item_type_id")
	public CustomItemType getItemType() {
		return itemType;
	}

	@ManyToOne(fetch = FetchType.LAZY, targetEntity = CustomItem.class)
	@JoinColumn(name = "parent_id")
	public CustomItem getParentItem() {
		return parentItem;
	}

	@OneToMany(fetch = FetchType.LAZY, targetEntity = CustomItem.class, mappedBy = "parentItem")
	@Cascade( { CascadeType.ALL })
	public Set<CustomItem> getChildrenSet() {
		return childrenSet;
	}

	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public void setItemDesc(String itemDesc) {
		this.itemDesc = itemDesc;
	}

	public void setItemType(CustomItemType itemType) {
		this.itemType = itemType;
	}

	public void setParentItem(CustomItem parentItem) {
		this.parentItem = parentItem;
	}

	public void setChildrenSet(Set<CustomItem> childrenSet) {
		this.childrenSet = childrenSet;
	}

	@Override
	@Transient
	public String getPropertyNameOfId() {
		return "itemId";
	}
}
